package cn.edu.sgu.www.pms.service.impl;

import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.dto.RoleDTO;
import cn.edu.sgu.www.pms.entity.Role;
import cn.edu.sgu.www.pms.mapper.RoleMapper;
import cn.edu.sgu.www.pms.service.RoleService;
import cn.edu.sgu.www.pms.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class RoleServiceImpl implements RoleService {

    private final RoleMapper roleMapper;

    @Autowired
    public RoleServiceImpl(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    @Override
    public void insert(RoleDTO roleDTO) {
        Role role = toEntity(roleDTO);

        role.setId(null);

        roleMapper.insert(role);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        roleMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(RoleDTO roleDTO) {
        ObjectUtils.requireNonNull(roleDTO.getId(), CustomMessages.ID_NOT_NULL);

        Role role = toEntity(roleDTO);

        roleMapper.updateById(role);
    }

    @Override
    public Role selectById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public List<Role> selectList() {
        return roleMapper.selectList(null);
    }

    /**
     * DTO转换位数据库实体类
     * @param roleDTO RoleDTO
     * @return Role
     */
    private Role toEntity(RoleDTO roleDTO) {
        Role role = new Role();

        role.setId(roleDTO.getId());
        role.setName(roleDTO.getName());
        role.setSort(roleDTO.getSort());
        role.setServiceId(roleDTO.getServiceId());
        role.setDescription(roleDTO.getDescription());

        return role;
    }

}